//
//  FQDSimpleWebViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/12/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDSimpleWebViewController.h"
#import "FQDWebToolbar.h"
#import "FQDDotLoadingView.h"
#import "MBProgressHUD+FQDExtension.h"
#import "UIViewController+FQDStatusBar.h"

#import "Masonry.h"

@interface FQDSimpleWebViewController () <UIWebViewDelegate>

// 底部工具条
@property (nonatomic, strong) FQDWebToolbar *toolbar;
// webView
@property (nonatomic, strong) UIWebView *webView;
// Loaidng界面
@property (nonatomic, strong) MBProgressHUD *loadingHUD;

@end

@implementation FQDSimpleWebViewController

- (instancetype)initWithUrlString:(NSString *)urlString
{
    self = [super init];
    if (self) {
        _urlString = urlString;
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.fqd_statusBarStyle = FQDStatusBarStyleLightTranslucent;
    [self setupWebToolbar];
    [self setupWebView];
}

- (void)setupWebToolbar
{
    self.toolbar = [[FQDWebToolbar alloc] init];
    [self.view addSubview:self.toolbar];
    
    [self.toolbar.popBackButton addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    [self.toolbar.refreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    [self.toolbar.backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [self.toolbar.forwardButton addTarget:self action:@selector(goForward) forControlEvents:UIControlEventTouchUpInside];
    [self.toolbar.shareButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    
    WeakSelf;
    [self.toolbar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@50);
    }];
}

- (void)setupWebView
{
    self.webView = [[UIWebView alloc] init];
    self.webView.delegate = self;
    self.webView.backgroundColor = FQDInfoWebViewBackgroundColor;
    [self.view addSubview:self.webView];
    
    [self loadUrlString:self.urlString];
    
    WeakSelf;
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.toolbar.mas_top);
    }];
}

- (void)startLoading
{
    if (!self.loadingHUD) {
        self.loadingHUD = [MBProgressHUD showLoadingAnimAddedTo:self.view];
        self.loadingHUD.color = [UIColor colorWithWhite:1.0f alpha:0.7f];
        self.loadingHUD.cornerRadius = 10.0f;
        self.loadingHUD.userInteractionEnabled = NO;
    }
}

- (void)endLoading
{
    if (self.loadingHUD) {
        self.loadingHUD.removeFromSuperViewOnHide = YES;
        [self.loadingHUD hide:YES];
        self.loadingHUD = nil;
    }
}

- (void)loadUrlString:(NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

#pragma mark - Event response


- (void)popBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)refresh
{
    [self.webView reload];
}

- (void)goBack
{
    [self.webView goBack];
}

- (void)goForward
{
    [self.webView goForward];
}

- (void)share
{
    FQDLog(@"share something...");
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self startLoading];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self endLoading];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self endLoading];
    FQDLog(@"InfoWebView fail to load:%@", [error localizedDescription]);
}

#pragma mark - Getters and Setters
- (void)setUrlString:(NSString *)urlString
{
    _urlString = urlString;
    
    [self loadUrlString:urlString];
}
@end
